package com.example.myapplication.Media;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.R;

public class AnimActivity extends AppCompatActivity {
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.media_anim_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("动画播放面板");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    SelectPanel();
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
        break;
    }
    return true;
  }

  private void GoBack() {
    finish();
  }

  private void SelectPanel() {
    LinearLayout frameLinearLayout = (LinearLayout) findViewById(R.id.frame_layout);
    LinearLayout tweenLinearLayout = (LinearLayout) findViewById(R.id.tween_layout);
    String title=getIntent().getStringExtra("title");
    if (title.equalsIgnoreCase("frame")) {
      tweenLinearLayout.setVisibility(View.GONE);
      SetFrameAction();
    } else if (title.equalsIgnoreCase("tween")) {
      frameLinearLayout.setVisibility(View.GONE);
      SetTweenAction();
    }
    getSupportActionBar().setTitle(title);
  }

  private void SetTweenAction() {
    ImageView imageView = (ImageView) findViewById(R.id.ImgView);
    Button alphaButton = (Button) findViewById(R.id.alpha_button);
    Button rotateButton = (Button) findViewById(R.id.rotate_button);
    Button scaleButton = (Button) findViewById(R.id.scale_button);
    Button transformButton = (Button) findViewById(R.id.transform_button);
    Button complexButton = (Button) findViewById(R.id.complex_button);

    alphaButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PlayFadeAnimation(imageView);
      }
    });
    rotateButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PlayRotateAnimation(imageView);
      }
    });
    scaleButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PlayScaleAnimation(imageView);
      }
    });
    transformButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PlayTransformAnimation(imageView);
      }
    });
    complexButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PlayComplexAnimation(imageView);
      }
    });
  }

  private void PlayComplexAnimation(ImageView imageView) {
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.combine_animation);
    imageView.startAnimation(animation);
  }

  private void PlayTransformAnimation(ImageView imageView) {
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_animation);
    imageView.startAnimation(animation);
  }

  private void PlayFadeAnimation(ImageView imageView) {
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_anim);
    //animation.setAnimationListener(new Animation.AnimationListener() {
    //  @Override public void onAnimationStart(Animation animation) {
    //
    //  }
    //
    //  @Override public void onAnimationEnd(Animation animation) {
    //    imageView.clearAnimation();
    //
    //
    //  }
    //
    //  @Override public void onAnimationRepeat(Animation animation) {
    //
    //  }
    //});
    imageView.startAnimation(animation);
  }

  private void PlayRotateAnimation(ImageView imageView) {
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_animation);

    imageView.startAnimation(animation);

  }
  private void PlayScaleAnimation(ImageView imageView) {
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
    imageView.startAnimation(animation);
  }

  private void SetFrameAction() {

    ImageView imageView = (ImageView) findViewById(R.id.ImgView);

    Button playButton = (Button) findViewById(R.id.play_frame_button);
    Button stopButton = (Button) findViewById(R.id.stop_frame_button);
    // 设置动画资源为 ImageView 的背景
    imageView.setBackgroundResource(R.drawable.animation_list);
    AnimationDrawable animationDrawable=(AnimationDrawable) imageView.getBackground();

    playButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        animationDrawable.start();
      }
    });
    stopButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (animationDrawable.isRunning()) {
          animationDrawable.stop();
        }
      }
    });
  }
}
